#include "debug.h"

NS_BEGIN(SDL)
NS_BEGIN(UTILS)

#pragma region structure Element

template<typename T>
AManager<T>::Element::Element(T* _object, IArgs* _datas, bool _delete)
	: Object(_object), Datas(_datas), Delete(_delete)
{
	CTOR("Element", this);
}

template<typename T>
AManager<T>::Element::~Element()
{
	DTOR("Element", this);
}

#pragma endregion
#pragma region class AManager
#pragma region CTOR & DTOR

template<typename T>
AManager<T>::AManager()
{
	CTOR("Manager", this);
}

template<typename T>
AManager<T>::AManager(AManager const&)
{
}

template<typename T>
AManager<T>::~AManager()
{
	DTOR("Manager", this);
	this->Clear();
}

#pragma endregion
#pragma region Operators

template<typename T>
AManager<T>& AManager<T>::operator=(AManager<T> const&)
{
	return (*this);
}

template<typename T>
T* AManager<T>::operator[](std::string const& _name)
{
	typename Elements::iterator it = m_elements.begin();
	for (; it != m_elements.end(); ++it)
		if (it->first.compare(_name) == 0)
			return (it->second.first->Object);
	return (NULL);
}

#pragma endregion

template<typename T>
void AManager<T>::Add(std::string const& _name, T* _element,
					  typename AManager<T>::MngrFunc _func, IArgs* _datas, bool _delete)
{
	if (_element == NULL)
		return ;
	typename Elements::iterator it = m_elements.begin();
	for (; it != m_elements.end(); ++it)
		if (it->first.compare(_name) == 0)
			return ;
	Element* e = new Element(_element, _datas, _delete);
	m_elements[_name] = Pair(e, _func);//Pair(_element, _func);
}

template<typename T>
void AManager<T>::Remove(std::string const& _name)
{
	typename Elements::iterator it = m_elements.find(_name);
	if (it == m_elements.end())
		return ;
	m_elements.erase(it);
	if (it->second.first->Delete == true)
	if (it->second.first->Delete == true)
	{
		delete it->second.first->Object;
		delete it->second.first->Datas;
	}
	delete it->second.first;
}

template<typename T>
void AManager<T>::Clear()
{
	typename Elements::iterator it = m_elements.begin();
	for (; it != m_elements.end(); ++it)
	{
		if (it->second.first->Delete == true)
		{
			delete it->second.first->Object;
			delete it->second.first->Datas;
		}
		delete it->second.first;
	}
	m_elements.clear();
}

template<typename T>
void AManager<T>::GetElements(std::list<typename AManager<T>::Element*>& _out)
{
	typename Elements::iterator it = m_elements.begin();
	for (; it != m_elements.end(); ++it)
		_out.push_back(it->second.first);
}

template<typename T>
typename AManager<T>::Element* AManager<T>::GetElement(std::string const& _name)
{
	typename Elements::iterator it = m_elements.find(_name);
	if (it == m_elements.end())
		return (NULL);
	return ((*it).second.first);
}

#pragma endregion

NS_END(UTILS)
NS_END(SDL)
